package crossbreed.factories;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextPane;
import javax.swing.text.DefaultHighlighter;
import crossbreed.diff.Diff;
import crossbreed.models.ComparisonModel.ComparisonModel;
import crossbreed.presenters.LeftFilePresenter;
import crossbreed.presenters.ComparisonPresenter;
import crossbreed.presenters.RightFilePresenter;
import crossbreed.utilities.ErrorLogger;
import crossbreed.utilities.SimpleFileReader;
import crossbreed.views.FileView;
import crossbreed.views.ComparisonView;
import crossbreed.views.IFileView;
import crossbreed.views.Controls.LineHighlighter;
import crossbreed.views.Controls.LineNumberPanel;

public class ComparisonFactory
{
	public JFrame createTwoWayMerge()
	{
		JPanel diffOverview = new JPanel();
		diffOverview.setPreferredSize(new Dimension(40, 1));
		
		JTextPane left = new JTextPane();
		JTextPane right = new JTextPane();
		LineHighlighter leftLineHighlighter = new LineHighlighter(new DefaultHighlighter());
		LineHighlighter rightLineHighlighter = new LineHighlighter(new DefaultHighlighter());
		left.setHighlighter(leftLineHighlighter);
		right.setHighlighter(rightLineHighlighter);
		
		
		JPanel leftPanel = new JPanel();
		leftPanel.setLayout(new BorderLayout());
		leftPanel.add(left, BorderLayout.CENTER);
		leftPanel.add(new LineNumberPanel(left), BorderLayout.WEST);
		
		JPanel rightPanel = new JPanel();
		rightPanel.setLayout(new BorderLayout());
		rightPanel.add(right, BorderLayout.CENTER);
		rightPanel.add(new LineNumberPanel(right), BorderLayout.WEST);
				
		JScrollPane scrollPaneLeft = new JScrollPane(leftPanel);
		JScrollPane scrollPaneRight = new JScrollPane(rightPanel);

		JSplitPane splitPane = new JSplitPane();
		splitPane.setLeftComponent(scrollPaneLeft);
		splitPane.setRightComponent(scrollPaneRight);
		splitPane.setDividerLocation(0.5);
		splitPane.setDividerSize(8);

		JLabel statusBar = new JLabel();
		statusBar.setPreferredSize(new Dimension(100, 18));

		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		panel.add(splitPane);
		panel.add(diffOverview, BorderLayout.LINE_START);
		panel.add(statusBar, BorderLayout.SOUTH);

		JMenuBar bar = new JMenuBar();
		bar.add(new JMenu("File"));
		bar.add(new JMenu("Edit"));
		bar.add(new JMenu("Help"));

		JFrame frame = new JFrame();
		frame.setTitle("Side-by-Side Merge");
		frame.setExtendedState(Frame.MAXIMIZED_BOTH);
		frame.add(panel);
		frame.setJMenuBar(bar);
		
		ComparisonModel model = new ComparisonModel(new Diff(),
				new ErrorLogger(), new SimpleFileReader(), new SimpleFileReader());
		model.setLeftFilePath("C:\\Users\\Aristal\\Desktop\\1.cs");
		model.setRightFilePath("C:\\Users\\Aristal\\Desktop\\2.cs");
		
		ComparisonPresenter mergePresenter = new ComparisonPresenter(model);
		ComparisonView mergeView = new ComparisonView(frame, splitPane, mergePresenter);
		mergePresenter.setView(mergeView);
		
		LeftFilePresenter leftPresenter = new LeftFilePresenter(model);
		IFileView leftView = new FileView(left, leftLineHighlighter, scrollPaneLeft);
		
		RightFilePresenter rightPresenter = new RightFilePresenter(model);
		IFileView rightView = new FileView(right, rightLineHighlighter, scrollPaneRight);
		
		leftPresenter.setView(leftView);
		leftPresenter.setRightView(rightView);
		scrollPaneLeft.getVerticalScrollBar().addAdjustmentListener(leftPresenter);
		
		rightPresenter.setView(rightView);
		rightPresenter.setLeftView(leftView);
		scrollPaneRight.getVerticalScrollBar().addAdjustmentListener(rightPresenter);

		return frame;
	}
}